﻿using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using TrafficArchives.Common;

namespace TrafficArchives.DataAccess
{
    /// <summary>
    /// 日志数据库操作。
    /// </summary>
    public class LogBb : DbBase
    {
        /// <summary>
        /// 读取日志数据。
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override PaginationData<DataTable> GetData(Request request)
        {
            var paginationData = new PaginationData<DataTable>();
            var normalSql = "select t.*, rownum as rowno from v_logs t where " + request.ToSql();
            var sql = request.Pagination.Enabled
                          ? string.Format("select * from ({0}) A where A.rowno > {1} and A.rowno <= {2}", normalSql,
                                          GetPageStart(request.Pagination), GetPageEnd(request.Pagination))
                          : normalSql;
            paginationData.Data = ExecuteDataTable(sql);
            if (request.Pagination.Enabled)
            {
                paginationData.RecordCount = ExecuteCount(normalSql.Replace("t.*, rownum as rowno", "count(*)"));
            }
            return paginationData;
        }

        /// <summary>
        /// 新增日志。
        /// </summary>
        /// <param name="data"></param>
        public override void Add(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = @"insert into LOGTABLE
(LOG_ID,CZR_ID,CZTYPENAME,CZTIME,CZMACHINEID,BCZRNAME,BCZRJSZH,BCZRDABH,BCZRSFZH)
values
(:LOG_ID,:CZR_ID,:CZTYPENAME,:CZTIME,:CZMACHINEID,:BCZRNAME,:BCZRJSZH,:BCZRDABH,:BCZRSFZH)";
            var parameters = new[]
                                 {
                                     MakeParam(":LOG_ID", OracleType.VarChar, 40, "LOG_ID"),
                                     MakeParam(":CZR_ID", OracleType.VarChar, 40, "CZR_ID"),
                                     MakeParam(":CZTYPENAME", OracleType.VarChar, 40, "CZTYPENAME"),
                                     MakeParam(":CZTIME", OracleType.DateTime, 40, "CZTIME"),
                                     MakeParam(":CZMACHINEID", OracleType.VarChar, 40, "CZMACHINEID"),
                                     MakeParam(":BCZRNAME", OracleType.VarChar, 40, "BCZRNAME"),
                                     MakeParam(":BCZRJSZH", OracleType.VarChar, 40, "BCZRJSZH"),
                                     MakeParam(":BCZRDABH", OracleType.VarChar, 40, "BCZRDABH"),
                                     MakeParam(":BCZRSFZH", OracleType.VarChar, 40, "BCZRSFZH")
                                 };
            OnAdd(data, sql, parameters);
        }

         /// <summary>
        /// 修改日志。
        /// </summary>
        /// <param name="data"></param>
        public override void Update(DataTable data)
         {
            if (data.Rows.Count == 0)
            {
                return;
            }
             const string sql =
                 @"update LOGTABLE
set CZR_ID=:CZR_ID, CZTYPENAME=:CZTYPENAME, CZTIME=:CZTIME, CZMACHINEID=:CZMACHINEID, 
BCZRNAME=:BCZRNAME, BCZRJSZH=:BCZRJSZH, BCZRDABH=:BCZRDABH, BCZRSFZH=:BCZRSFZH where LOG_ID=:LOG_ID";
            var parameters = new[]
                                 {
                                     MakeParam(":CZTYPENAME", OracleType.VarChar, 40, "CZTYPENAME"),
                                     MakeParam(":CZTIME", OracleType.DateTime, 40, "CZTIME"),
                                     MakeParam(":CZMACHINEID", OracleType.VarChar, 40, "CZMACHINEID"),
                                     MakeParam(":BCZRNAME", OracleType.VarChar, 40, "BCZRNAME"),
                                     MakeParam(":BCZRJSZH", OracleType.VarChar, 40, "BCZRJSZH"),
                                     MakeParam(":BCZRDABH", OracleType.VarChar, 40, "BCZRDABH"),
                                     MakeParam(":BCZRSFZH", OracleType.VarChar, 40, "BCZRSFZH"),
                                     MakeParam(":LOG_ID", OracleType.VarChar, 40, "LOG_ID"),
                                     MakeParam(":CZR_ID", OracleType.VarChar, 40, "CZR_ID")
                                 };
            OnUpdate(data, sql, parameters);
        }

        /// <summary>
        /// 删除日志数据。
        /// </summary>
        /// <param name="ids"></param>
        public override void Delete(List<string> ids)
        {
            const string sql = "delete from LOGTABLE t where t.LOG_ID in ('{0}')";
            ExecuteNoneQuery(string.Format(sql, string.Join("','", ids)));
        }
    }
}